package com.softeem.hbsmvideo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softeem.hbsmvideo.entity.Category;
import com.softeem.hbsmvideo.entity.User;
import com.softeem.hbsmvideo.entity.Video;
import com.softeem.hbsmvideo.service.CategoryService;
import com.softeem.hbsmvideo.service.UserService;
import com.softeem.hbsmvideo.service.VideoService;
import com.softeem.hbsmvideo.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/*
* 视频的业务接口
* */
@RequestMapping("/video")
@RestController
public class VideoController {
    @Autowired
    private VideoService videoService;
    @Autowired
    private UserService userService;
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    public R list(@RequestParam(defaultValue = "1")Integer page,@RequestParam(defaultValue = "5") Integer limit){
        List<Video> list = videoService.list(new Page<Video>(page, limit));

        list.forEach(v->{               //循环遍历
            //获取作者id
            Integer uid=v.getUid();
            //获取频道id
            Integer cid=v.getCid();
            //根据作者id查询作者
            User user = userService.getById(uid);
            Category category= categoryService.getById(cid);
            v.setUser(user);
            v.setCategory(category);
        });

        Long count=videoService.count();
        return R.ok("查询成功").setData(list).setCount(count);

    }


}
